using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for GroupList.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:CategoryList runat=server></{0}:CategoryList>")]
	public class CategoryList : System.Web.UI.WebControls.WebControl
	{
		#region Properties
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string url;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URL 
		{
			get
			{
				return url;
			}

			set
			{
				url = value;
			}
		}

		private MessageBoard.Group group;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Group Group 
		{
			get
			{
				return group;
			}

			set
			{
				group = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private string imagesFolder;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ImagesFolder 
		{
			get
			{
				return imagesFolder;
			}

			set
			{
				imagesFolder = value;
			}
		}
		
		private string categorySecurityUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string CategorySecurityUrl 
		{
			get
			{
				return categorySecurityUrl;
			}

			set
			{
				categorySecurityUrl = value;
			}
		}

		private string forumSecurityUrl;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string ForumSecurityUrl 
		{
			get
			{
				return forumSecurityUrl;
			}

			set
			{
				forumSecurityUrl = value;
			}
		}

		private string addCategoryURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string AddCategoryURL 
		{
			get
			{
				return addCategoryURL;
			}

			set
			{
				addCategoryURL = value;
			}
		}

		private string editCategoryURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string EditCategoryURL 
		{
			get
			{
				return editCategoryURL;
			}

			set
			{
				editCategoryURL = value;
			}
		}

		private string deleteCategoryURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string DeleteCategoryURL 
		{
			get
			{
				return deleteCategoryURL;
			}

			set
			{
				deleteCategoryURL = value;
			}
		}

		private string addForumURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string AddForumURL 
		{
			get
			{
				return addForumURL;
			}

			set
			{
				addForumURL = value;
			}
		}

		private string editForumURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string EditForumURL 
		{
			get
			{
				return editForumURL;
			}

			set
			{
				editForumURL = value;
			}
		}

		private string deleteForumURL;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string DeleteForumURL 
		{
			get
			{
				return deleteForumURL;
			}

			set
			{
				deleteForumURL = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}
		#endregion

		#region Translate
		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("CategoryList");

		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			CurrentUser.MakeRequest();

			// Main Table
			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellpadding", "0");
			output.WriteAttribute("cellspacing", "0");
			output.RenderBeginTag(HtmlTextWriterTag.Tr);
			output.WriteBeginTag("td");
			output.WriteAttribute("class", "Forum_TableBorder");
			output.Write(HtmlTextWriter.TagRightChar);

			// Table head
			output.WriteBeginTag("table");
			output.WriteAttribute("width", "100%");
			output.WriteAttribute("cellspacing", "1");
			output.WriteAttribute("cellpadding", "2");
				output.RenderBeginTag(HtmlTextWriterTag.Tr);

					// Title
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("colspan", "2");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Title"));
					output.WriteEndTag("td");

					// Threads
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "80");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Threads"));
					output.WriteEndTag("td");
					
					// Posts
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "80");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Posts"));
					output.WriteEndTag("td");

					// Last Post
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "150");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write(Translate("Last Post"));
					output.WriteEndTag("td");

					// Actions
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("width", "80");
					output.WriteAttribute("align", "center");
					output.Write(HtmlTextWriter.TagRightChar);
						if(group.WithCategories==true)
						{
							output.Write(MainItems());
						}
						else
						{
							output.Write(CategoryItems((MessageBoard.Category)MessageBoard.Category.GetAll(group)[0]));
						}
					output.WriteEndTag("td");

				output.RenderEndTag();
				int numberOfCategories = 0;
				int numberOfForums=0;

				// Displaying Categories
				foreach(MessageBoard.Category category in MessageBoard.Category.GetAll(Group))
				{
					if(MessageBoard.Role.Check(currentUser, category, MessageBoard.Role.Permission.CanViewCategories) ==  MessageBoard.Role.Allow.Yes)
					{
						numberOfCategories++;
						if(group.WithCategories==true)
						{
							// Catrgory title - description
							output.RenderBeginTag(HtmlTextWriterTag.Tr);
							output.WriteBeginTag("td");
							output.WriteAttribute("colspan", "5");
							output.WriteAttribute("class", "Forum_Title2");
							output.Write(HtmlTextWriter.TagRightChar);
							output.WriteBeginTag("span");
							output.WriteAttribute("class", "Forum_BoldText");
							output.Write(HtmlTextWriter.TagRightChar);
							output.Write(category.Title);
							output.WriteEndTag("span");
							output.Write(" - " + category.Description);
							output.WriteEndTag("td");

							// Actions
							output.WriteBeginTag("td");
							output.WriteAttribute("class", "Forum_Title2");
							output.Write(HtmlTextWriter.TagRightChar);
							output.Write(CategoryItems(category));
							output.WriteEndTag("td");

							output.RenderEndTag();
							numberOfForums=0;
						}

						// Displaying Forums
						foreach(MessageBoard.Forum forum in MessageBoard.Forum.GetAll(category))
						{
							if(MessageBoard.Role.Check(currentUser, forum, MessageBoard.Role.Permission.CanViewForums) ==  MessageBoard.Role.Allow.Yes)
							{
								numberOfForums++;
								output.RenderBeginTag(HtmlTextWriterTag.Tr);

								// Filder Icon
								output.WriteBeginTag("td");
								output.WriteAttribute("class", "Forum_Title3");
								output.WriteAttribute("width", "20");
								output.WriteAttribute("align", "center");
								output.WriteAttribute("vAlign", "top");
								output.Write(HtmlTextWriter.TagRightChar);
								if(forum.Threads>10)
								{
									output.Write("<img src="+imagesFolder+"/hotclosed.gif border=0>");
								}
								else
								{
									output.Write("<img src="+imagesFolder+"/closed.gif border=0>");
								}
								output.WriteEndTag("td");

								// Forum Title
								output.WriteBeginTag("td");
								output.WriteAttribute("class", "Forum_Title4");
								output.Write(HtmlTextWriter.TagRightChar);
								output.WriteBeginTag("a");
								output.WriteAttribute("href", URL+"Forum=ThreadList&ForumId="+forum.Id);
								output.WriteAttribute("class", "Forum_BoldLink");
								output.Write(HtmlTextWriter.TagRightChar);
								output.Write(forum.Title);
								output.WriteEndTag("a");
								output.Write("<br>");
								output.Write(forum.Description);
								output.WriteEndTag("td");

								// Number of threads
								output.WriteBeginTag("td");
								output.WriteAttribute("class", "Forum_Title3");
								output.WriteAttribute("align", "center");
								output.Write(HtmlTextWriter.TagRightChar);
								output.Write(forum.Threads);
								output.WriteEndTag("td");

								// Number of posts
								output.WriteBeginTag("td");
								output.WriteAttribute("class", "Forum_Title4");
								output.WriteAttribute("align", "center");
								output.Write(HtmlTextWriter.TagRightChar);
								output.Write(forum.Posts);
								output.WriteEndTag("td");

								// Latest post
								output.WriteBeginTag("td");
								output.WriteAttribute("class", "Forum_Title3");
								output.WriteAttribute("align", "center");
								output.Write(HtmlTextWriter.TagRightChar);
								if(forum.LatestThread!=null)
								{
									output.Write(forum.LatestThread.LatestPost.PostDate.ToString(), "{0:dd MMM yyyy h:m tt}");
									output.Write("<br>"+Translate("by")+ " " + forum.LatestThread.LatestPost.User.DisplayName);
								}
								output.WriteEndTag("td");

								// Actions
								output.WriteBeginTag("td");
								output.WriteAttribute("class", "Forum_Title4");
								output.WriteAttribute("align", "center");
								output.Write(HtmlTextWriter.TagRightChar);
								output.Write(ForumItems(forum));
								output.WriteEndTag("td");

								output.RenderEndTag();
							}
						}

						// if no forums found
						if(numberOfForums==0)
						{
							output.RenderBeginTag(HtmlTextWriterTag.Tr);
							output.WriteBeginTag("td");
							output.WriteAttribute("class", "Forum_Title3");
							output.WriteAttribute("colspan", "6");
							output.Write(HtmlTextWriter.TagRightChar);
							if(group.WithCategories==true)
							{
								output.Write(Translate("No forums found in this category."));
							}
							else
							{
								output.Write(Translate("No forums found in this group."));
							}
							output.WriteEndTag("td");
							output.RenderEndTag();
						}
					}
				}
				// if no categories
				if(numberOfCategories==0 && group.WithCategories==true)
				{
					output.RenderBeginTag(HtmlTextWriterTag.Tr);
						output.WriteBeginTag("td");
						output.WriteAttribute("class", "Forum_Title3");
						output.WriteAttribute("colspan", "6");
						output.Write(HtmlTextWriter.TagRightChar);
							output.Write(Translate("No categories found in this group."));
						output.WriteEndTag("td");
					output.RenderEndTag();

				}

				// Bottom bar
				output.RenderBeginTag(HtmlTextWriterTag.Tr);
					output.WriteBeginTag("td");
					output.WriteAttribute("class", "Forum_Title1");
					output.WriteAttribute("colspan", "6");
					output.Write(HtmlTextWriter.TagRightChar);
						output.Write("&nbsp;");
					output.WriteEndTag("td");
				output.RenderEndTag();


			output.WriteEndTag("table");

			output.WriteEndTag("td");
			output.RenderEndTag();
			output.WriteEndTag("table");
		}

		private string MainItems()
		{
			string str = "";
			if(MessageBoard.Role.Check(currentUser, group, MessageBoard.Role.Permission.CanAddCategories) ==  MessageBoard.Role.Allow.Yes)
			{
				str = "<a href='"+addCategoryURL+"Forum=AddCategory&GroupId="+Group.Id+"'><img src="+imagesFolder+"/group_edit.gif border=0 title='"+Translate("Add new category")+"'></a> ";
			}
			return str;
		}

		private string CategoryItems(MessageBoard.Category category)
		{
			string str = "";
			if(group.WithCategories==true)
			{
				if(MessageBoard.Role.Check(currentUser, category, MessageBoard.Role.Permission.CanManageCategoriesSecurity) == MessageBoard.Role.Allow.Yes)
				{
					str += "<a href='"+categorySecurityUrl+"Forum=ManageSecurity&CategoryId=" + category.Id + "'><img src="+imagesFolder+"/Permissions.gif border=0 title='"+Translate("Category Security")+"'></a> ";
				}
				if(MessageBoard.Role.Check(currentUser, category, MessageBoard.Role.Permission.CanEditCategories) == MessageBoard.Role.Allow.Yes)
				{
					str += "<a href='"+editCategoryURL+"Forum=EditCategory&CategoryId="+category.Id+"'><img src="+imagesFolder+"/group_edit.gif border=0 title='"+Translate("Edit this category")+"'></a> ";
				}
				if(MessageBoard.Role.Check(currentUser, category, MessageBoard.Role.Permission.CanDeleteCategories) == MessageBoard.Role.Allow.Yes)
				{
					str += "<a href='"+deleteCategoryURL+"Forum=DeleteCategory&CategoryId="+category.Id+"'><img src="+imagesFolder+"/group_delete.gif border=0 title='"+Translate("Delete this category")+"'></a> ";	
				}
				if(MessageBoard.Role.Check(currentUser, category, MessageBoard.Role.Permission.CanAddForums) == MessageBoard.Role.Allow.Yes)
				{
					str += "<a href='"+AddForumURL+"Forum=AddForum&CategoryId="+category.Id+"'><img src="+imagesFolder+"/group_edit.gif border=0 title='"+Translate("Add forum in this category")+"'></a> ";
				}
			}
			else
			{
				if(MessageBoard.Role.Check(currentUser, group, MessageBoard.Role.Permission.CanAddForums) == MessageBoard.Role.Allow.Yes)
				{
					str += "<a href='"+AddForumURL+"Forum=AddForum&CategoryId="+category.Id+"'><img src="+imagesFolder+"/group_edit.gif border=0 title='"+Translate("Add forum in this group")+"'></a> ";
				}
			}
			return str;
		}

		private string ForumItems(MessageBoard.Forum forum)
		{
			string str = "";
			if(MessageBoard.Role.Check(currentUser, forum, MessageBoard.Role.Permission.CanManageForumsSecurity) == MessageBoard.Role.Allow.Yes)
			{
				str += "<a href='"+forumSecurityUrl+"Forum=ManageSecurity&ForumId=" + forum.Id + "'><img src="+imagesFolder+"/Permissions.gif border=0 title='"+Translate("Forum Security")+"'></a> ";
			}
			if(MessageBoard.Role.Check(currentUser, forum, MessageBoard.Role.Permission.CanEditForums) == MessageBoard.Role.Allow.Yes)
			{
				str += "<a href='"+editForumURL+"Forum=EditForum&GroupId="+group.Id+"&ForumId="+forum.Id+"'><img src="+imagesFolder+"/group_edit.gif border=0 title='"+Translate("Edit this forum")+"'></a> ";
			}
			if(MessageBoard.Role.Check(currentUser, forum, MessageBoard.Role.Permission.CanDeleteForums) == MessageBoard.Role.Allow.Yes)
			{
				str += "<a href='"+deleteForumURL+"Forum=DeleteForum&GroupId="+group.Id+"&ForumId="+forum.Id+"'><img src="+imagesFolder+"/group_delete.gif border=0 title='"+Translate("Delete this forum")+"'></a> ";
			}
			return str;
		}
	}
}
